/**
 * 父类 element对象
 */
class Element {
  constructor (options) {
    const opts = options || {}
    // 设置坐标设尺寸
    this.x = opts.x
    this.y = opts.y
    this.width = opts.width
    this.height = opts.height
    this.speed = opts.speed
  }
  move (x, y) {
    const addX = x || 0
    const addY = y || 0
    this.x += addX
    this.y += addY
  }
  /**
   * 方法：hasCrash 判断是否撞到当前元素
   */
  hasCrash (target) {
    let crash = false
    // 判断四边是否都没有空隙
    if (!(this.x + this.width < target.x) &&
      !(target.x + target.width < this.x) &&
      !(this.y + this.height < target.y) &&
      !(target.y + target.height < this.y)) {
      // 碰撞了
      crash = true
    }
    return crash
  }

  /**
   * 方法：hasHit 判断子弹是否击中目标
   */
  hasHit (target) {
    const bullets = this.bullets
    let hasHit = false
    for (let j = bullets.length - 1; j >= 0; j--) {
      // 如果子弹击中目标，则销毁子弹
      if (bullets[j].hasCrash(target)) {
        bullets.splice(j, 1)
        hasHit = true
        break
      }
    }
    return hasHit
  }
  /**
   * 方法：booming 爆炸中
   */
  booming (boomLimit, callback) {
    this.status = 'booming'
    this.boomCount += 1
    if (this.boomCount > boomLimit) {
      this.status = 'boomed'
      if (callback) {
        callback()
      }
    }
    return this
  }
}

export default Element
